package com.lebang.schedule;

import com.lebang.component.RedisClient;
import com.lebang.component.RedisSdkClient;
import com.lebang.model.enums.buss.DownCategory;
import com.lebang.service.DownAppService;
import com.lebangweb.common.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Set;

import static com.lebang.common.validcode.ValidCodeUtil.LOG_OUT_CODE;
import static com.lebang.controller.api.LuckDrawController.LUCKDRAW_HASH_MEMBER;
import static com.lebang.controller.api.TaskClickController.newHandKey;
import static com.lebang.service.AppAdControService.APP_AD_COUNT_KEY;
import static com.lebang.service.AppAdControService.SDK_HASH;
import static com.lebang.service.AppRouseAdService.COUNT_KEY;
import static com.lebang.service.DownAppService.*;
import static com.lebang.service.MemberInviteRebateRecordService.REBATE_HASH_MEMBER;
import static com.lebang.service.buss.MemberService.MEMBER_LOGIN_RECORD;
import static com.lebang.service.sys.AdErrorConfigService.ERROR_SDK_KEY;
import static com.lebang.controller.api.bus.BusLoginApi.BUS_MEMBER_KEY;

/**
 * @Author: xwj
 * @Date: 2020/7/2 09:21
 * @Version 1.0
 */
@Slf4j
@Component
public class ExpireTimeTask {
    @Autowired
    private RedisSdkClient redisSdkClient;
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private DownAppService downAppService;
    /**
     * 每天4点对指定的key设置过期时间
     */
    @Scheduled(cron = "0 0 4 * * ?")
    public void execute() {

        if(redisClient.tryGetDistributedLock("lock:Scheduled:expiretime", UUIDUtil.M(),100000)) {

        //获取昨日的日期
        LocalDate nowDate = LocalDate.now().plusDays(-1);
        String nowDateString = nowDate.format(DateTimeFormatter.ISO_DATE);

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        int todayTime = Integer.parseInt(nowDate.format(df));


        //获取昨日24小时
        LocalDateTime time = LocalDateTime.of(nowDate, LocalTime.MIN);
        for (int i = 0; i <= 23; i++) {
            //每日必赚增加每日次数
            redisClient.del(CSJ_RECORD_COUNT + time.plusHours(i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH")));
        }

        //活跃用户分渠道
        Set<String> memberLoginRecords = redisClient.keys(MEMBER_LOGIN_RECORD + Integer.parseInt(nowDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"))) + ":"+"*");
        memberLoginRecords.forEach(s-> redisClient.del(s));


        //当日用户每日必赚完成数
        Set<String> dayEarnsCounts = redisClient.keys(DAY_EARNS_COUNT+"*");
        Set<String> commonDayEarnsCounts = redisSdkClient.keys(DAY_EARNS_COUNT+"*");
        dayEarnsCounts.forEach(s-> redisClient.del(s));
        commonDayEarnsCounts.forEach(s-> redisSdkClient.del(s));

        Set<String> appAdCounts = redisClient.keys(nowDateString + ":" + APP_AD_COUNT_KEY+"*");
        appAdCounts.forEach(s-> redisClient.expire(s,86400));

        //当日广告播放量过期时间 广告位
        Set<String> countKeys = redisClient.keys(nowDateString + ":" + COUNT_KEY+"*");
        countKeys.forEach(s-> redisClient.del(s));

        redisClient.delOne("FAIL_COUNT:"+nowDateString);
        //每日必赚任务类型 浏览任务  下载任务
        redisClient.del(CSJ_RECORD_COUNT + nowDateString + ":" + DownCategory.LOOK.getIndex());
        redisClient.del(CSJ_RECORD_COUNT + nowDateString + ":" + DownCategory.DOWN_LOAD.getIndex());
        //当日注销验证短信次数过期时间
        redisClient.del(LOG_OUT_CODE + nowDateString);
        //当日广告播放量过期时间
        redisClient.del(nowDateString + ":" + COUNT_KEY);
        //当日广告播放量过期时间(增加广告位观看次数)
        redisClient.expire(nowDateString + ":" + APP_AD_COUNT_KEY,86400);
        redisClient.expire(nowDateString + ":" + SDK_HASH,86400);
        //新手任务点击过期时间3天(统计用到，不删)
        redisClient.expire(newHandKey + nowDate.format(DateTimeFormatter.ofPattern("yyyyMMdd")), 259200);
        //当日用户每日必赚完成数过期时间
            redisSdkClient.del(DAY_EARNS_COUNT + nowDateString);
        redisClient.del(DAY_EARNS_COUNT + nowDateString);
        //当日师徒收益存入缓存过期时间
        redisClient.del(REBATE_HASH_MEMBER + nowDateString);
        //当日抽奖次数过期时间
        redisClient.del(LUCKDRAW_HASH_MEMBER + nowDateString);
        //每日必赚今日总成功数过期时间
        redisClient.del(DAY_EARNS_ALL_COUNT + nowDateString);
        redisClient.del( downAppService.getFailKey());
        redisClient.del(COUNT_KEY+nowDateString);

        //每日最大邀请数
        redisClient.del("INVITE_MAX_NUM:" + todayTime);


        //刪除用戶昨日进度
        redisClient.del( "RouseDayCount:"+ nowDateString);
        //删除请求日志的队列
        //redisClient.del(Constant.LOG_QUEUE);

        //错误码数
        redisClient.del(ERROR_SDK_KEY + nowDateString);
        redisClient.del(BUS_MEMBER_KEY+ todayTime);
            redisClient.del("MAIDAN"+nowDateString);
            log.info("过期时间设置完成");
        }else {
            log.info("其他服务执行");
        }



    }

}
